package com.yuexunit.operationman.nbsy.module.nurse.ui;

import android.os.Bundle;
import android.view.View;
import android.widget.ImageView;
import android.widget.TextView;

import com.alibaba.fastjson.JSON;
import com.bumptech.glide.Glide;

import com.yuexunit.operationman.nbsy.R;
import com.yuexunit.operationman.nbsy.base.BaseKJActivity;
import com.yuexunit.operationman.nbsy.base.SubmitBean;
import com.yuexunit.operationman.nbsy.config.AppConfig;
import com.yuexunit.operationman.nbsy.module.SafeOperation.bean.SignBean;
import com.yuexunit.operationman.nbsy.module.SafeOperation.model.impl.SafeOperationSignModel;
import com.yuexunit.operationman.nbsy.module.SafeOperation.presenter.ISafeOperationSignPresenter;
import com.yuexunit.operationman.nbsy.module.SafeOperation.presenter.impl.SafeOperationSignPresenter;
import com.yuexunit.operationman.nbsy.module.SafeOperation.ui.view.ISafeOperationSignView;
import com.yuexunit.operationman.nbsy.module.nurse.Bean.NurseBean;
import com.yuexunit.operationman.nbsy.module.nurse.model.impl.NurseModel;
import com.yuexunit.operationman.nbsy.module.nurse.presenter.INursePresenter;
import com.yuexunit.operationman.nbsy.module.nurse.presenter.impl.NursePresenter;
import com.yuexunit.operationman.nbsy.module.nurse.ui.view.INurseView;
import com.yuexunit.operationman.nbsy.widget.ActionSheetDialog;
import com.yuexunit.operationman.nbsy.widget.LoadingDialog;
import com.yuexunit.operationman.nbsy.widget.LoginDialog;

import org.kymjs.kjframe.ui.ViewInject;
import org.kymjs.kjframe.utils.PreferenceHelper;

import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.HashMap;
import java.util.Locale;
import java.util.Map;

import butterknife.Bind;
import butterknife.ButterKnife;
import butterknife.OnClick;

/**
 * Created by work-jx on 2018/1/3.
 */
public class Act_Ops_Sign extends BaseKJActivity implements ISafeOperationSignView, INurseView {
    @Bind(R.id.tv_patient_name)
    TextView tvPatientName;
    @Bind(R.id.tv_patient_sex)
    TextView tvPatientSex;
    @Bind(R.id.tv_patient_visitno)
    TextView tvPatientVisitno;
    @Bind(R.id.tv_patient_disease)
    TextView tvPatientDisease;

    @Bind(R.id.img_ra_sign_01)
    ImageView imgRaSign01;
    @Bind(R.id.btn_ra_sign_01)
    TextView btnRaSign01;
    @Bind(R.id.tv_ra_sign_01_time)
    TextView tvRaSign01Time;
    @Bind(R.id.tv_ra_sign_01_name)
    TextView tvRaSign01Name;

    @Bind(R.id.img_ra_sign_02)
    ImageView imgRaSign02;
    @Bind(R.id.btn_ra_sign_02)
    TextView btnRaSign02;
    @Bind(R.id.tv_ra_sign_02_time)
    TextView tvRaSign02Time;
    @Bind(R.id.tv_ra_sign_02_name)
    TextView tvRaSign02Name;

    @Bind(R.id.img_ra_sign_03)
    ImageView imgRaSign03;
    @Bind(R.id.btn_ra_sign_03)
    TextView btnRaSign03;
    @Bind(R.id.tv_ra_sign_03_time)
    TextView tvRaSign03Time;
    @Bind(R.id.tv_ra_sign_03_name)
    TextView tvRaSign03Name;

    @Bind(R.id.img_ra_sign_04)
    ImageView imgRaSign04;
    @Bind(R.id.btn_ra_sign_04)
    TextView btnRaSign04;
    @Bind(R.id.tv_ra_sign_04_time)
    TextView tvRaSign04Time;
    @Bind(R.id.tv_ra_sign_04_name)
    TextView tvRaSign04Name;

    ISafeOperationSignPresenter presenter_sign;
    INursePresenter presenter;
    LoginDialog loginDialog;
    LoadingDialog loadingDialog;
    private int type = -1;

    @Override
    public void setRootView() {
        setContentView(R.layout.act_ops_sign);
        ButterKnife.bind(this);
    }

    @Override
    public void initData() {
        super.initData();
        loginDialog = new LoginDialog(aty);
        loadingDialog = new LoadingDialog(aty);
        presenter = new NursePresenter(this, new NurseModel());
        presenter_sign = new SafeOperationSignPresenter(this, new SafeOperationSignModel());
        setTitle();
        setContent();
    }

    private void setTitle() {
        tvPatientName.setText(AppConfig.NURSEBEAN.getObject().getNursing().getPatientName());
        if (AppConfig.NURSEBEAN.getObject().getNursing().getPatientSex().equals("1")) {
            tvPatientSex.setText("男");
        } else if (AppConfig.NURSEBEAN.getObject().getNursing().getPatientSex().equals("2")) {
            tvPatientSex.setText("女");
        } else {
            tvPatientSex.setText("不详");
        }
        tvPatientVisitno.setText("住院号" + AppConfig.NURSEBEAN.getObject().getNursing().getInHospitalNo());
        tvPatientDisease.setText(AppConfig.NURSEBEAN.getObject().getNursing().getOperationName());
    }

    private void setContent() {
        for (NurseBean.ObjectEntity.NursingItemListEntity p : AppConfig.NURSEBEAN.getObject().getNursingItemList()) {
            switch (p.getItemCode()) {
                case "WJB_Doctor_Sign":
                    if (p.getItemValue().length() > 0) {
                        imgRaSign01.setVisibility(View.VISIBLE);
                        btnRaSign01.setVisibility(View.GONE);
                        tvRaSign01Time.setVisibility(View.VISIBLE);
                        tvRaSign01Name.setVisibility(View.VISIBLE);
                        Glide.with(aty)
                                .load(p.getItemValue())
                                .placeholder(R.color.white)
                                .error(R.color.white)
                                .into(imgRaSign01);
                        tvRaSign01Time.setText("签名时间:" + p.getItemOther());
                        tvRaSign01Name.setText(p.getItemName());
                    }
                    break;
                case "WJB_Nurse_Sign":
                    if (p.getItemValue().length() > 0) {
                        imgRaSign04.setVisibility(View.VISIBLE);
                        btnRaSign04.setVisibility(View.GONE);
                        tvRaSign04Time.setVisibility(View.VISIBLE);
                        tvRaSign04Name.setVisibility(View.VISIBLE);
                        Glide.with(aty)
                                .load(p.getItemValue())
                                .placeholder(R.color.white)
                                .error(R.color.white)
                                .into(imgRaSign04);
                        tvRaSign04Time.setText("签名时间:" + p.getItemOther());
                        tvRaSign04Name.setText(p.getItemName());
                    }
                    break;
                case "SP_Over_Sign":
                    if (p.getItemValue().length() > 0) {
                        imgRaSign02.setVisibility(View.VISIBLE);
                        btnRaSign02.setVisibility(View.GONE);
                        tvRaSign02Time.setVisibility(View.VISIBLE);
                        tvRaSign02Name.setVisibility(View.VISIBLE);
                        Glide.with(aty)
                                .load(p.getItemValue())
                                .placeholder(R.color.white)
                                .error(R.color.white)
                                .into(imgRaSign02);
                        tvRaSign02Time.setText("签名时间:" + p.getItemOther());
                        tvRaSign02Name.setText(p.getItemName());
                    }
                    break;
                case "AP_Over_Sign":
                    if (p.getItemValue().length() > 0) {
                        imgRaSign03.setVisibility(View.VISIBLE);
                        btnRaSign03.setVisibility(View.GONE);
                        tvRaSign03Time.setVisibility(View.VISIBLE);
                        tvRaSign03Name.setVisibility(View.VISIBLE);
                        Glide.with(aty)
                                .load(p.getItemValue())
                                .placeholder(R.color.white)
                                .error(R.color.white)
                                .into(imgRaSign03);
                        tvRaSign03Time.setText("签名时间:" + p.getItemOther());
                        tvRaSign03Name.setText(p.getItemName());
                    }
                    break;
            }
        }
    }

    @OnClick({R.id.titlebar_docadvice_img_back, R.id.btn_ra_sign_01,
            R.id.btn_ra_sign_02, R.id.btn_ra_sign_03, R.id.btn_ra_sign_04})
    public void onViewClicked(View view) {
        switch (view.getId()) {
            case R.id.titlebar_docadvice_img_back:
                finish();
                break;
            case R.id.btn_ra_sign_01:
                showTypeDialog();
                type = 1;
                break;
            case R.id.btn_ra_sign_02:
                showTypeDialog();
                type = 2;
                break;
            case R.id.btn_ra_sign_03:
                showTypeDialog();
                type = 3;
                break;
            case R.id.btn_ra_sign_04:
                showTypeDialog();
                type = 4;
                break;
        }
    }

    private void showTypeDialog() {
        ActionSheetDialog actionSheetDialog;
        actionSheetDialog = new ActionSheetDialog(this).builder()
                .setCancelable(true)
                .setCanceledOnTouchOutside(true);
        actionSheetDialog.addSheetItem("HIS签名", ActionSheetDialog.SheetItemColor.Blue,
                new ActionSheetDialog.OnSheetItemClickListener() {
                    @Override
                    public void onClick(int which) {
                        loginDialog.builder();
                        loginDialog.show();
                        loginDialog.setCallBack(new LoginDialog.ICallBack() {
                            @Override
                            public void callback(Bundle bundle) {
                                Map<String, String> params = new HashMap<>();
                                params.put("jobNum", bundle.getString("person_num"));
                                params.put("loginPwd", bundle.getString("person_psw"));
                                presenter_sign.getSignInfo(params);
                            }
                        });
                    }
                });
        actionSheetDialog.addSheetItem("虹膜签名(暂不可用)", ActionSheetDialog.SheetItemColor.Blue,
                new ActionSheetDialog.OnSheetItemClickListener() {
                    @Override
                    public void onClick(int which) {

                    }
                });
        actionSheetDialog.show();
    }

    @Override
    public void insertNurseDate(SubmitBean submitBean) {
        if (submitBean.getCode() == 200) {
            ViewInject.toast("保存成功");
            finish();
        } else {
            ViewInject.toast(submitBean.getDesc());
        }
    }

    @Override
    public void updateNurseDate(SubmitBean submitBean) {
        if (submitBean.getCode() == 200) {
            ViewInject.toast("保存成功");
            finish();
        } else {
            ViewInject.toast(submitBean.getDesc());
        }
    }

    @Override
    public void getSignInfo(SignBean signBean) {
        loginDialog.dismiss();
        if (signBean.getCode() == 200) {
            SimpleDateFormat formatter = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss", Locale.getDefault());
            Date curDate = new Date(System.currentTimeMillis());//获取当前时间
            String str = formatter.format(curDate);
            switch (type) {
                case 1:
                    imgRaSign01.setVisibility(View.VISIBLE);
                    Glide.with(this)
                            .load(signBean.getObject().getSignPicture())
                            .placeholder(R.color.white)
                            .error(R.color.white)
                            .into(imgRaSign01);
                    btnRaSign01.setVisibility(View.GONE);
                    tvRaSign01Time.setVisibility(View.VISIBLE);
                    tvRaSign01Time.setText("签名时间:" + str);
                    tvRaSign01Name.setVisibility(View.VISIBLE);
                    tvRaSign01Name.setText(signBean.getObject().getDoctorName());

                    for (int i = 0; i < AppConfig.NURSEBEAN.getObject().getNursingItemList().size(); i++) {
                        if (AppConfig.NURSEBEAN.getObject().getNursingItemList().get(i).getItemCode().equals("WJB_Doctor_Sign")) {
                            AppConfig.NURSEBEAN.getObject().getNursingItemList().get(i).setItemValue(signBean.getObject().getSignPicture());
                            AppConfig.NURSEBEAN.getObject().getNursingItemList().get(i).setItemName(signBean.getObject().getDoctorName());
                            AppConfig.NURSEBEAN.getObject().getNursingItemList().get(i).setItemOther(str);
                        }
                    }
                    break;
                case 2:
                    imgRaSign02.setVisibility(View.VISIBLE);
                    Glide.with(this)
                            .load(signBean.getObject().getSignPicture())
                            .placeholder(R.color.white)
                            .error(R.color.white)
                            .into(imgRaSign02);
                    btnRaSign02.setVisibility(View.GONE);
                    tvRaSign02Time.setVisibility(View.VISIBLE);
                    tvRaSign02Time.setText("签名时间:" + str);
                    tvRaSign02Name.setVisibility(View.VISIBLE);
                    tvRaSign02Name.setText(signBean.getObject().getDoctorName());

                    for (int i = 0; i < AppConfig.NURSEBEAN.getObject().getNursingItemList().size(); i++) {
                        if (AppConfig.NURSEBEAN.getObject().getNursingItemList().get(i).getItemCode().equals("SP_Over_Sign")) {
                            AppConfig.NURSEBEAN.getObject().getNursingItemList().get(i).setItemValue(signBean.getObject().getSignPicture());
                            AppConfig.NURSEBEAN.getObject().getNursingItemList().get(i).setItemName(signBean.getObject().getDoctorName());
                            AppConfig.NURSEBEAN.getObject().getNursingItemList().get(i).setItemOther(str);
                        }
                    }
                    break;
                case 3:
                    imgRaSign03.setVisibility(View.VISIBLE);
                    Glide.with(this)
                            .load(signBean.getObject().getSignPicture())
                            .placeholder(R.color.white)
                            .error(R.color.white)
                            .into(imgRaSign03);
                    btnRaSign03.setVisibility(View.GONE);
                    tvRaSign03Time.setVisibility(View.VISIBLE);
                    tvRaSign03Time.setText("签名时间:" + str);
                    tvRaSign03Name.setVisibility(View.VISIBLE);
                    tvRaSign03Name.setText(signBean.getObject().getDoctorName());

                    for (int i = 0; i < AppConfig.NURSEBEAN.getObject().getNursingItemList().size(); i++) {
                        if (AppConfig.NURSEBEAN.getObject().getNursingItemList().get(i).getItemCode().equals("AP_Over_Sign")) {
                            AppConfig.NURSEBEAN.getObject().getNursingItemList().get(i).setItemValue(signBean.getObject().getSignPicture());
                            AppConfig.NURSEBEAN.getObject().getNursingItemList().get(i).setItemName(signBean.getObject().getDoctorName());
                            AppConfig.NURSEBEAN.getObject().getNursingItemList().get(i).setItemOther(str);
                        }
                    }
                    break;
                case 4:
                    imgRaSign04.setVisibility(View.VISIBLE);
                    Glide.with(this)
                            .load(signBean.getObject().getSignPicture())
                            .placeholder(R.color.white)
                            .error(R.color.white)
                            .into(imgRaSign04);
                    btnRaSign04.setVisibility(View.GONE);
                    tvRaSign04Time.setVisibility(View.VISIBLE);
                    tvRaSign04Time.setText("签名时间:" + str);
                    tvRaSign04Name.setVisibility(View.VISIBLE);
                    tvRaSign04Name.setText(signBean.getObject().getDoctorName());

                    for (int i = 0; i < AppConfig.NURSEBEAN.getObject().getNursingItemList().size(); i++) {
                        if (AppConfig.NURSEBEAN.getObject().getNursingItemList().get(i).getItemCode().equals("WJB_Nurse_Sign")) {
                            AppConfig.NURSEBEAN.getObject().getNursingItemList().get(i).setItemValue(signBean.getObject().getSignPicture());
                            AppConfig.NURSEBEAN.getObject().getNursingItemList().get(i).setItemName(signBean.getObject().getDoctorName());
                            AppConfig.NURSEBEAN.getObject().getNursingItemList().get(i).setItemOther(str);
                        }
                    }
                    break;
            }

            if (AppConfig.NURSEBEAN.getObject().getNursing().getNursingId() == null) {
                Map<String, String> params = new HashMap<>();
                params.put("operationNo", PreferenceHelper.readString(aty, AppConfig.PATIENT_INFO, AppConfig.PATIENT_OPERATION_ID) + "");
                params.put("saveData", JSON.toJSONString(AppConfig.NURSEBEAN));
                presenter.insertNurseDate(params);
            } else {
                Map<String, String> params = new HashMap<>();
                params.put("accountId", PreferenceHelper.readInt(aty, AppConfig.USER_INFO, AppConfig.USER_ACCOUNT) + "");
                params.put("fullName", PreferenceHelper.readString(aty, AppConfig.USER_INFO, AppConfig.USER_FULLNAME) + "");
                params.put("saveData", JSON.toJSONString(AppConfig.NURSEBEAN));
                presenter.updateNurseDate(params);
            }
        } else {
            ViewInject.toast(signBean.getDesc());
        }
    }

    @Override
    public void loadingDialogStatus(int status) {
        switch (status) {
            case AppConfig.SHOW_DIALOG:
                loadingDialog.setCanceledOnTouchOutside(false);
                loadingDialog.show();
                break;
            case AppConfig.DISMISS_DIALOG:
                loadingDialog.dismiss();
                break;

        }
    }

}
